<!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta charset="utf-8" />
<title>kcwebplus</title>
<meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=no"/>
<meta name="HandheldFriendly" content="true"/>
<meta name="MobileOptimized" content="320"/>
<%include file="../include/static.html"/>
<style>
*{padding:0px;margin:0px}
.el-header, .el-footer {background-color: #B3C0D1;line-height: 60px;padding:0px;}
.kcw-side-scroll::-webkit-scrollbar{width:0px}
</style>
</head>
<body>
<div id="app" class="background">
    <div style="height:33px;padding-top:10px;margin:4px auto;width:100%">
        &nbsp&nbsp
        <el-button-group>
            <el-input size="mini" style="width:200px;float:left" placeholder="项目名称" v-model="kw"></el-input>
            <el-button size="mini" icon="el-icon-search" @click="data.pagenow=1;getlist()" type="success">搜索</el-button>
            <el-button size="mini" type="success"  @click="admindialog={status:true,title:'添加项目'},admin={id:'',title:'',icon:'',descs:'',paths:'',filename:'',types:'',other:''}">添加项目</el-button>
        </el-button-group>
        <el-button icon="el-icon-download" size="mini" @click="backxz('app/intapp/controller/index/common/file')" style="float:left">下载数据</el-button>
        <el-upload class="upload-demo" action="/intapp/index/setup/upunback?paths=app/intapp/controller/index/common/file" :show-file-list="false" accept="*.zip" :on-success="onsuccess" :on-error="onerror" style="float:left">
            <el-button icon="el-icon-upload" size="mini">上传intapp_index.zip</el-button>
        </el-upload>
    </div>
    <div style="margin:0 auto;width:100%">
            <el-table :data="data.lists" :height="winheight-100" @selection-change="handleSelectionChange">
                <el-table-column type="selection" width="55"></el-table-column>
                <el-table-column prop="icon" label="" width="50">
                    <template slot-scope="scope">
                        <img v-if="scope.row.icon" :src="scope.row.icon" alt="" style="height:30px">
                    </template>
                </el-table-column>
                <el-table-column prop="title" label="项目名称" width="160">
                    <template slot-scope="scope"><el-tag size="small" style="background:#FFF;color:#000">{{scope.row.title}}</el-tag></template>
                </el-table-column>
                <el-table-column prop="" label="运行状态" width="280">
                    <template slot-scope="scope">
                        <span v-if="scope.row.status==1">
                            <i class="fa fa-play" style="color:#67C23A;"></i>运行中 &nbsp
                            <el-button type="danger" @click="restart(scope.row,'stop')" size="mini" plain style="height:20px;padding-top:4px">停止</el-button>
                            <el-button type="danger" @click="restart(scope.row,'restart')" size="mini" plain style="height:20px;padding-top:4px">重启</el-button>
                        </span>
                        <span v-else>
                            <i class="fa fa-pause" style="color:red;"></i>已停止 &nbsp<el-button plain type="primary" style="height:20px;padding-top:4px" @click="restart(scope.row,'start')" size="mini">启动</el-button>
                        </span>
                        <el-button type="text" @click="logpythonrun(scope.row)">运行日志</el-button>
                    </template>
                </el-table-column>
                <el-table-column prop="addtime" label="添加时间" width="140">
                    <template slot-scope="scope">
                        <span v-html="time_date(scope.row.addtime)"></span>
                    </template>
                </el-table-column>
                <el-table-column label="" width="200">
                <template slot-scope="scope">
                    <el-button @click="self.admindialog.status=true,self.admin=scope.row" size="mini">设置</el-button>
                    <el-button @click="addstart(scope.row)" size="mini">加入开机启动</el-button>
                </template>
                </el-table-column>
                <el-table-column prop="descs" label="" width="260">
                    <template slot-scope="scope"><el-tag size="small" style="background:#FFF;color:#000">{{scope.row.descs}}</el-tag></template>
                </el-table-column>
                <el-table-column prop="" label="运行文件">
                    <template slot-scope="scope">
                        <el-tag v-if="scope.row.status==1" size="small" type="success">{{scope.row.paths}}/{{scope.row.filename}}</el-tag>
                        <el-tag v-else size="small" type="warning">{{scope.row.paths}}/{{scope.row.filename}}</el-tag>
                    </template>
                </el-table-column>
            </el-table>
            <div class="block" style="background:#FFF;margin-top:4px;height:36px;padding-top:4px">
                <span style="float:left;margin-top:2px">&nbsp&nbsp&nbsp<el-button size="mini" type="danger" @click="deletes" icon="el-icon-delete">删除选中</el-button>&nbsp</span>
                <el-pagination background
                    @size-change="handleSizeChange" @current-change="handleCurrentChange"
                    :current-page="5" :page-size="data.pagesize"
                    layout="total, sizes, prev, pager, next, jumper" :total="data.count">
                </el-pagination>
            </div>
    </div>
    <el-dialog
    :title="admindialog.title"
    :visible.sync="admindialog.status"
    width="500px">
    <el-form ref="form" :model="admin" label-width="80px" label-position="left" size="mini">
    <div style="padding:20px">
        <el-form-item label="项目名称">
            <el-input placeholder="项目名称" v-model="admin.title"></el-input>
        </el-form-item>
        <el-form-item label="图标">
            <el-input placeholder="图标地址" v-model="admin.icon" style="width:70%"></el-input>
            <el-button @click="openurl('/intapp/imgmt?select=1')" size="mini">选择图片</el-button>
        </el-form-item>
        <el-form-item label="项目描述">
            <el-input placeholder="项目描述" v-model="admin.descs"></el-input>
        </el-form-item>
        <el-form-item label="运行目录">
            <el-input v-if="admin.paths" placeholder="运行目录" v-model="admin.paths" disabled style="width:80%;float:left"></el-input>
            <!-- <div style="float:left" id="paths"></div> -->
            <img @click="openurl('/intapp/file/?select=1&paths='+admin.paths)" title="选择目录" src="https://img.kwebapp.cn/icon/folder.png" style="height:26px;float:left;margin-left:10px;margin-top:0px;cursor:pointer">
        </el-form-item>
        <el-form-item label="运行文件">
            <el-input v-if="admin.filename" placeholder="运行文件" v-model="admin.filename" disabled style="width:80%;float:left"></el-input>
            <img @click="openurl('/intapp/file/?select=1&paths='+admin.paths)" title="选择运行文件" src="https://img.kwebapp.cn/icon/folder.png" style="height:26px;float:left;margin-left:10px;margin-top:0px;cursor:pointer">
        </el-form-item>
        <el-form-item label="运行环境">
            <el-select v-model="admin.types" placeholder="请选择">
                <el-option key="python3.6" label="python3.6解释器" value="python3.6">python3.6解释器</el-option>
                <el-option key="php7.2" label="php7.2解释器" value="php7.2">php7.2解释器</el-option>
                <el-option key="php7.3" label="php7.3解释器" value="php7.3">php7.3解释器</el-option>
            </el-select><br>
            <span v-if="admin.types=='php7.2'" style="color:red">您必须提前在<el-button type="text" @click="openurl('/intapp/soft?kw=php')">“软件管理”</el-button>中安装php7.2，已安装请忽略，其他安装方式将无效</span>
            <span v-if="admin.types=='php7.3'" style="color:red">您必须提前在<el-button type="text" @click="openurl('/intapp/soft?kw=php')">“软件管理”</el-button>中安装php7.3，已安装请忽略，其他安装方式将无效</span>
        </el-form-item>
        <el-form-item label="运行参数">
            <el-input  placeholder="运行参数" v-model="admin.other"></el-input>
        </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="admindialog.status = false">取 消</el-button>
            <el-button v-if="admin.id" type="primary" @click="setpythonrun">修改并运行</el-button>
            <el-button v-else type="primary" @click="setpythonrun">添加并运行</el-button>
        </span>
    </div>
    </el-dialog>
    <el-dialog title="日志" :visible.sync="logpythonrunstatus" width="1000px">
            <pre :style="'height:'+(winheight-300)+'px;overflow-y:auto;background:#000;color:#FFF;'">{{runlog}}</pre>
    </el-dialog>
</div>
</body>
<script>
var VUE=null
function setimgmt(domain,paths,item){
    mysetimgmt(domain,paths,item)
    layer.closeAll();
}
function setfile(paths,item){
    layer.closeAll();
    setadmin(paths,item)
}
VUE = new Vue({
    el: '#app',
    data: {
        winheight:document.documentElement.clientHeight,winwidth:document.documentElement.clientWidth,
        admin:{id:'',title:'',icon:'',descs:'',paths:'',filename:"",types:'',other:''},admindialog:{status:false,title:'添加管理'},kcwebtype:'web',
        kw:'',
        data:{
            'pagesize':20,
            'count':0,
            'pagenow':1,
            'lists':[]
        },Change:[],
        startdata:{value:'',name:'',types:'shell',icon:''},
        logpythonrunstatus:false,runlog:'',
    },
    mounted:function(){
        self=this
        window.onresize = function(){
            self.winheight=document.documentElement.clientHeight
            self.winwidth=document.documentElement.clientWidth
        }
        window.mysetimgmt = this.mysetimgmt;
        window.setadmin = this.setadmin;
        self.getlist()
    },
    methods: {
        backxz:function(paths){
            var self=this;
            window.location.href="/intapp/index/setup/backxz/intapp_index.zip?paths="+paths;
        },onsuccess:function(response, file, fileList){
			var self=this;
			self.$message({type: 'success',message: '上传成功'});
		},onerror:function(){
			var self=this;
			self.$message({type: 'error',message: '上传失败'});
		},
        logpythonrun:function(item){
            var self=this
            self.logpythonrunstatus=true;
            self.runlog="日志加载中..."
            self.get("/intapp/index/setup/logpythonrun/"+item.id).then(function(res){
				self.runlog=res.data
			}).catch(function(err){
                self.runlog="加载失败"
            });
        },
        mysetimgmt:function(domain,paths,item){
            var self=this
            if (item.suffix=='jpg' || item.suffix=='JPG' || item.suffix=='png' || item.suffix=='PNG' || item.suffix=='gif' || item.suffix=='GIF'){
                self.admin.icon=domain+paths+item.name
            }else{
                self.$message({type: 'error',message: '请选择图片文件'});
            }
        },
        addstart:function(item){
			var self=this
            var interpreter=md5(item['paths']+item['types']+item['filename']+item['other'])
            self.startdata.name=item.title
            self.startdata.icon=item.icon
            if(item['other']){
                self.startdata.value="cd "+item['paths']+" && nohup "+interpreter+" "+item['filename']+" "+item['other']+"  > "+item['paths']+"/"+interpreter+".log 2>&1 &"
            }else{
                self.startdata.value="cd "+item['paths']+" && nohup "+interpreter+" "+item['filename']+" > "+item['paths']+"/"+interpreter+".log 2>&1 &"
            }
			self.post("/intapp/index/setup/addstart",self.startdata,"请稍后...").then(function(res){
				self.$message({type: 'success',message: '加入成功'}); return
			})
		},
        restart:function(item,types){
            var self=this
            if(types=='restart'){
                self.post("/intapp/index/setup/restart/stop",item).then(function(res){
                    item.status=0
                    self.post("/intapp/index/setup/restart/start",item,"启动中...").then(function(res){
                        item.status=1
                        self.$message({type: 'success',message: '重启成功'}); return
                    })
                })
            }else{
                self.post("/intapp/index/setup/restart/"+types,item,"请稍后...").then(function(res){
                    if(types=='stop'){
                        item.status=0
                    }else{
                        item.status=1
                    }
                    self.$message({type: 'success',message: '成功'}); return
                })
            }
        },
        setadmin:function(paths,item){
            if (item.types=='file'){
                this.admin.filename=item.name
            }else{
                this.admin.paths=paths+item.name
            }
            
        },
        openurl:function(url){
            layer.open({
                type: 2,title: false,shadeClose: true,shade: 0.8,
                area: ['80%', '90%'],content:url
            }); 
        },
        getlist:function(){
            self=this
			self.get("/intapp/index/setup/pythonrulists",{'kw':self.kw}).then(function(res){
				self.data=res.data
			})
        },
        setpythonrun:function(){
			self=this
            if(!self.admin.types||!self.admin.paths||!self.admin.filename||!self.admin.title){
                self.$message({type: 'error',message: '输入不全!'}); return
            }
			self.post("/intapp/index/setup/setpythonrun",self.admin,"请稍后...").then(function(res){
				self.admindialog.status=false
				self.getlist()
			}).catch(function(err){
                self.admindialog.status=false
            })
		},
        deletes:function(){
			self=this
			if(!self.Change.length){
				self.$message.error('您未选择任何内容');
			}else{
				self.$confirm('此操作将永久删除该记录, 是否继续?', '删除提醒', {
				confirmButtonText: '删除',
				cancelButtonText: '取消',
				type: 'warning'
				}).then(function(){
					id=[]
					for(var i=0;i<self.Change.length;i++){
						id.push(self.Change[i].id)
					}
					self.post("/intapp/index/setup/delpythonrun",id,"正在删除中...").then(function(res){
						self.data.pagenow=1
						self.getlist()
						self.$message({type: 'success',message: '选中记录已删除!'});
					})
				}).catch(function(){});
			}
		},
       
        handleSelectionChange:function(val) {
            self=this
			self.Change=val
		},
        handleSizeChange:function(val) {
            self=this
			self.data.pagesize=val
			self.getlist()
		},
        handleCurrentChange:function(val) {
			self.data.pagenow=val
			self.getlist()
		}
    }
});
</script> 
</html>
